package com.stockz.watchlist.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.view.StockChartView;
import com.stockz.watchlist.manager.EntryManager;
import com.stockz.watchlist.manager.WatchListManager;
import com.stockz.watchlist.model.Entry;

@Component
public class AddToWatchListPanel extends JPanel {
	private static final long serialVersionUID = 2466223481219046001L;
	private static final String createText = "Create New And Add";
	private static final String addButtonText = "Add";
	
	@Autowired
	public AddToWatchListPanel(final WatchListManager watchListManager,
			final EntryManager entryManager, final StockChartView view) {
		final JComboBox watchListNamesComboBox = new JComboBox();
		List<String> allWatchListName = watchListManager.getAllWatchListName();
		if(allWatchListName != null && !allWatchListName.isEmpty()){
			for(String name : allWatchListName){
				watchListNamesComboBox.addItem(name);
			}
		}
		watchListNamesComboBox.addItem(createText);
		add(watchListNamesComboBox);
		
		JButton addButton = new JButton(addButtonText);
		addButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				if(createText.equalsIgnoreCase(watchListNamesComboBox.getSelectedItem().toString())){
					//TODO code to create a new watchlist
				}else{
					Entry entry = new Entry();
					entry.setScrip(view.getSelectedStockChart().getStockChartPanel().getStockChartPanelState().getScrip());
					entry.setWatchList(watchListManager.get(watchListNamesComboBox.getSelectedItem().toString()));
					entryManager.persist(entry);
				}
			}
		});
	}
}
